package com.workmanager.service;

import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
import com.workmanager.bean.*;
import com.workmanager.mapper.TeacherMessageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by haocongshun on 2016/8/29.
 */
@Service("messageTeacherService")
public class MessageTeacherService {
    @Resource(name = "teacherMessageMapper")
    private TeacherMessageMapper teacherMessageMapper;

    public TeacherMessageMapper getTeacherMessageMapper() {
        return teacherMessageMapper;
    }

    public void setTeacherMessageMapper(TeacherMessageMapper teacherMessageMapper) {
        this.teacherMessageMapper = teacherMessageMapper;
    }

    public Map<String ,Object> getall(String teacher_id){
        Map<String ,Object> mapMessage = new HashMap<String ,Object>();
        List<SystemMessageBean> listSystemMessage = getSystemMessage();
       // List<OneFeedbackBean> listOneFeedbackMessage = getOneFeedbackMessage(teacher_id);
        List<FeedbackBean> listFeedbackMessage = getFeedbackMessage(teacher_id);
        List<WorkBean> listWorkMessage = getWorkMessage(teacher_id);
        mapMessage.put("systemMsg",listSystemMessage);
        mapMessage.put("feedbackMsg",listFeedbackMessage);
        mapMessage.put("workMsg",listWorkMessage);
        return mapMessage;
    }

    /***
     * 获取系统通知消息
     * 系统通知消息列表@return
     */
    public List<SystemMessageBean> getSystemMessage(){
       return teacherMessageMapper.getSystemMessage();
    }

    /***
     * 获取给定老师的作业反馈信息
     * 教师id@param teacher_id
     * 封装为一次对话的一个反馈信息集合@return
     */
    public List<OneFeedbackBean> getOneFeedbackMessage(String teacher_id){
        //声明一个封装为一次对话的一个反馈信息集合
        List<OneFeedbackBean> listOneFeedbackMessage = new ArrayList<OneFeedbackBean>();
        List<FeedbackBean> listFeedbackMessage = teacherMessageMapper.getFeedbackMessage(teacher_id);
        for(FeedbackBean feedbackBean : listFeedbackMessage){
            boolean sessionIsExists = false;
            for(OneFeedbackBean oneFeedbackBean : listOneFeedbackMessage){
                //判断同一作业的对话信息是否存在
                if(oneFeedbackBean.getWork_id() == feedbackBean.getWork_id()){
                    sessionIsExists = true;
                }
            }
            //如果这次对话不存在，新建对话，添加进入对话集合
            if(!sessionIsExists){
                listOneFeedbackMessage.add(new OneFeedbackBean(feedbackBean.getWork_id()));
            }
            for(OneFeedbackBean oneFeedbackBean : listOneFeedbackMessage){
                //如果这次对话存在,将对话信息添加进入对话
                if(oneFeedbackBean.getWork_id() == feedbackBean.getWork_id()){
                    sessionIsExists = true;
                    Map<String ,String > message = oneFeedbackBean.getContent();
                    message.put(String.valueOf(oneFeedbackBean.getNum()),feedbackBean.getFeedback_time()+"#"+feedbackBean.getFeedback_content());
                    oneFeedbackBean.setNum(oneFeedbackBean.getNum()+1);
                    oneFeedbackBean.setStudent_name(feedbackBean.getaWork().getaStudent().getStudent_name());
                    oneFeedbackBean.setContent(message);
                }
            }

        }
        return listOneFeedbackMessage;
    }
    public List<FeedbackBean> getFeedbackMessage(String teacher_id){
        return teacherMessageMapper.getFeedbackMessage(teacher_id);
    }
    /***
     * 获取作业上交信息
     * 教师id@param teacher_id
     * 作业提交信息集合列表@return
     */
    public List<WorkBean> getWorkMessage(String teacher_id){
        return teacherMessageMapper.getWorkMessage(teacher_id);
    }
    public boolean answer(EvaluateBean evaluate){
        evaluate.setEvaluate_id(teacherMessageMapper.getEvaluateKey());
        return teacherMessageMapper.answer(evaluate)==1?true:false;
    }
}
